package com.shareach.http.shop.rpc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shareach.dao.redis.impl.MatchRDao;
import com.shareach.dao.redis.impl.SnatchRDao;
import com.shareach.domain.BaseCityDomain;
import com.shareach.domain.BizTuanDomain;
import com.shareach.domain.BizTuanShopDomain;
import com.shareach.domain.DiscountType;
import com.shareach.http.shop.parameter.BizTuanParameter;
import com.shareach.http.shop.parameter.BizTuanShopParameter;
import com.shareach.service.BaseCityService;
import com.shareach.service.BizTuanService;
import com.shareach.service.BizTuanShopService;
import com.shareach.util.DateUtils;
import com.shareach.util.Globals;
import com.shareach.util.HelperUtil;
import com.shareach.web.basic.Answer;
import com.shareach.web.basic.Ask;
import com.shareach.web.basic.IAskParameter;
import com.shareach.web.basic.IAskRPC;

@Service("tuanUploadRpc")
public class TuanUploadRpc implements IAskRPC {
	@Autowired
	private BizTuanService bizTuanService;
	@Autowired
	private BizTuanShopService bizShopTuanService;
	@Autowired
	private BaseCityService baseCityService;
	@Autowired
	private SnatchRDao snatchRDao;
	@Autowired
	private MatchRDao matchRDao;
	final Logger logger = Logger.getLogger("quartz");
	@Override
	public Answer<?> run(Ask ask, String ip) throws Exception {
		BizTuanParameter p = (BizTuanParameter) getParameterMap(ask);
		Answer<Long> result = this.getAnswer();
		
		if (StringUtils.isEmpty(p.title)) {
			result.setCode(-1);
			result.setMsg("该条数据没有title,不处理");
			logger.info("该条数据没有title,不处理:::"+ask.getSn());
			return result;
		}
		// 用来判断是否采集
		snatchRDao.setSnatchedSiteId(p.city, p.site, p.tuanId);

		if (p.shopes == null || p.shopes.size() == 0) {
			result.setCmd(ask.getCmd());
			result.setCode(-1);
			result.setMsg("没有商家的团购无法保存");
		} else {
			BizTuanDomain tuan = new BizTuanDomain();
			tuan.setCity(Long.parseLong(p.city));
			tuan.setDescry(p.descr);
			tuan.setDiscount(p.discount);
			if (StringUtils.isNotBlank(p.end)) {
				tuan.setEnd(DateUtils.parse(p.end));
			}
			tuan.setImg(p.img);
			tuan.setOrgPrice(p.orgPrice);
			tuan.setPrice(p.price);
			tuan.setSite(p.site);
			tuan.setSiteId(p.tuanId);

			tuan.setStatus(1);
			tuan.setTitle(p.title);
			tuan.setShopCount(p.shopes.size());

			Map<String, Object> rettuanMap = bizTuanService.saveOrUpdate(tuan);
			String retTuanCode = (String) rettuanMap.get(Globals.GLOBAL_SERVICE_RET_CODE);
			if (!retTuanCode.equals(Globals.GLOBAL_SERVICE_CODE_ERROR)) {
				Long tuanId = (Long) rettuanMap.get(Globals.GLOBAL_SERVICE_RET_ID);

				BaseCityDomain city = baseCityService.getById(Long.parseLong(p.city));
				for (BizTuanShopParameter ti : p.shopes) {
					BizTuanShopDomain tuanShop = new BizTuanShopDomain();
					tuanShop.setAddr(ti.addr);
					tuanShop.setBrandName(ti.brandName);
					tuanShop.setName(ti.name);
					tuanShop.setOrgName(ti.orgName);
					tuanShop.setShopId(ti.shopId);
					tuanShop.setSite(p.site);
					tuanShop.setSiteId(p.tuanId);
					tuanShop.setCity(Long.parseLong(p.city));
					if (ti.tels != null && !ti.tels.isEmpty()) {
						List<String> tels = new ArrayList<String>();
						for (String t : ti.tels) {
							if (StringUtils.isBlank(t))
								continue;
							List<String> telCodes = HelperUtil.getTels(city.getTel(), t);
							for (String tel : telCodes) {
								if (StringUtils.isBlank(tel))
									continue;
								tels.add(t);
							}
						}
						if (tels.size() > 0) {
							tuanShop.setTels(tels.toArray(new String[tels.size()]));
						}
					}
					tuanShop.setTuanId(tuanId);
					Map<String, Object> retMap = bizShopTuanService.saveOrUpdate(tuanShop);
					String retCode = (String) retMap.get(Globals.GLOBAL_SERVICE_RET_CODE);

					// 匹配队列使用
					if (retCode.equals(Globals.GLOBAL_SERVICE_CODE_NOTEXIST)) {
						matchRDao.addDiscountShop(DiscountType.Tuan, tuanShop.getId());
					}
				}
			}
		}
		return result;
	}

	@Override
	public Answer<Long> getAnswer() {
		return new Answer<Long>();
	}

	@Override
	public IAskParameter getParameterMap(Ask ask) {
		BizTuanParameter parameter = new BizTuanParameter();
		HashMap<String, Object> map = ask.getRequest();
		parameter.parseFromMap(map);
		return parameter;
	}
}
